package person.learn.restful;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import person.learn.util.JsonResponse;

import java.util.ArrayList;
import java.util.List;

/**
 * restful api 写法测试
 * Created by barton on 16-4-19.
 */
@RestController
@RequestMapping("/v1/fruits")
public class FruitController {

    @RequestMapping(value = "", method = RequestMethod.GET)
    public ResponseEntity<List<Fruit>> list() {

        List<Fruit> fruits = new ArrayList<>();

        Fruit apple = new Fruit();
        apple.setName("Apple");
        apple.setShape("circle");

        Fruit orange = new Fruit();
        orange.setName("Orange");
        orange.setShape("circle");

        fruits.add(apple);
        fruits.add(orange);

        return new ResponseEntity<>(fruits, HttpStatus.OK);
    }

//    @RequestMapping(value = "", method = RequestMethod.GET)
//    public ResponseEntity<JSONObject> list() {
//
//        List<Fruit> fruits = new ArrayList<>();
//
//        Fruit apple = new Fruit();
//        apple.setName("Apple");
//        apple.setShape("circle");
//
//        Fruit orange = new Fruit();
//        orange.setName("Orange");
//        orange.setShape("circle");
//
//        fruits.add(apple);
//        fruits.add(orange);
//
//        JSONObject json = new JSONObject();
//        json.put("fruits", fruits);
//
//        return new ResponseEntity<>(json, HttpStatus.OK);
//    }

    @RequestMapping(value = "{name}", method = RequestMethod.GET)
    public ResponseEntity<JsonResponse<Fruit>> getOne(@PathVariable String name) {

        System.out.println("传递进入的name:" + name);

        JsonResponse<Fruit> jsonResponse;

        switch (name) {
            case "orange":
                Fruit orange = new Fruit();
                orange.setName("Orange");
                orange.setShape("circle");
                jsonResponse = new JsonResponse<>();
                jsonResponse.setResult(orange);
                return new ResponseEntity<>(jsonResponse, HttpStatus.OK);
            case "fruit":
                Fruit apple = new Fruit();
                apple.setName("Apple");
                apple.setShape("circle");

                jsonResponse = new JsonResponse<>();
                jsonResponse.setSuccessMsg("请求成功");
                jsonResponse.setResult(apple);
                return new ResponseEntity<>(jsonResponse, HttpStatus.OK);
            default:
                jsonResponse = new JsonResponse<>();
                jsonResponse.setResult(null);
                jsonResponse.setSuccessMsg("未查询到相关记录");
                return new ResponseEntity<>(jsonResponse,HttpStatus.OK);
        }
    }
}


class Fruit {
    private String name;
    private String shape;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getShape() {
        return shape;
    }

    public void setShape(String shape) {
        this.shape = shape;
    }
}
